import numpy as np
import matplotlib
matplotlib.use('TkAgg')#TkAgg是图形后端
import matplotlib.pyplot as plt
import seaborn as sns

# 设置seaborn的风格
sns.set(style="whitegrid")

# 生成数据
x = np.linspace(-10, 10, 400)
y_relu = np.maximum(0, x)

# 绘制ReLU函数
plt.figure(figsize=(8, 6))
plt.plot(x, y_relu, linewidth=2, color='royalblue')
plt.title('ReLU Activation Function', fontsize=18, fontweight='bold', color='darkblue')
plt.xlabel('Input (x)', fontsize=14)
plt.ylabel('ReLU(x)', fontsize=14)
plt.axhline(0, color='black',linewidth=0.7)
plt.axvline(0, color='black',linewidth=0.7)
plt.grid(True, linestyle='--', alpha=0.6)
plt.show()



# 生成数据
y_sigmoid = 1 / (1 + np.exp(-x))

# 绘制Sigmoid函数
plt.figure(figsize=(8, 6))
plt.plot(x, y_sigmoid, linewidth=2, color='seagreen')
plt.title('Sigmoid Activation Function', fontsize=18, fontweight='bold', color='darkgreen')
plt.xlabel('Input (x)', fontsize=14)
plt.ylabel('Sigmoid(x)', fontsize=14)
plt.axhline(0, color='black',linewidth=0.7)
plt.axvline(0, color='black',linewidth=0.7)
plt.grid(True, linestyle='--', alpha=0.6)
plt.show()


# 设置seaborn的风格
sns.set(style="whitegrid")

# Softmax函数实现
def softmax(x):
    orig_shape = x.shape
    if len(x.shape) > 1:
        # Matrix
        constant_shift = np.max(x, axis=1).reshape(1, -1)
        x -= constant_shift
        x = np.exp(x)
        normlize = np.sum(x, axis=1).reshape(1, -1)
        x /= normlize
    else:
        # Vector
        constant_shift = np.max(x)
        x -= constant_shift
        x = np.exp(x)
        normlize = np.sum(x)
        x /= normlize
    assert x.shape == orig_shape
    return x

# Softmax输入
softmax_inputs = np.arange(-10, 10, 0.1)  # 改为0.1步长增加平滑度
softmax_outputs = softmax(softmax_inputs)

# 绘制图形
plt.figure(figsize=(10, 6))
plt.plot(softmax_inputs, softmax_outputs, color='dodgerblue', linewidth=2, label='Softmax Output')

# 添加标题和标签
plt.title('Softmax Activation Function', fontsize=18, fontweight='bold', color='darkblue')
plt.xlabel('Input Values', fontsize=14)
plt.ylabel('Softmax Output', fontsize=14)

# 设置网格
plt.grid(True, linestyle='--', alpha=0.6)

# 显示图例
plt.legend(fontsize=12)

# 展示图像
plt.tight_layout()
plt.show()